import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login/Login.vue')
  },

  {
    path: '/',
    // name: 'layout',
    // 如果有子路由就不要给父路由起名字了
    component: () => import('../views/Layout/Layout.vue'),
    children:[{
      path: '',
      // redirect:'/home',
      name: 'home',
      component: () => import('../views/Home/Home.vue')
    },
    {
      path:'/article',
      name:'article',
      component:()=>import('../views/Article/Article.vue')
    },{
      path:'/publish',
      name:'publish',
      component:()=>import('../views/Publish/Publish.vue'),
      // props:true
    },
    {
      path:'/image',
      name:'image',
      component:()=>import('../views/Image/Image.vue')
    }
  ]
  }
]
const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next)=>{
  const user=JSON.parse(window.localStorage.getItem('user'))
  if(to.path!=='/login'){
    if(user){
      next()
    }else{
      next('/login')
    }
  }else{
    next()
  }
})

export default router
